#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MCP 计算器服务基础工具示例

本文件展示了计算器服务的基础数学运算工具实现，包括：
- 加法工具
- 减法工具
- 乘法工具
- 除法工具
- 取模工具
- 幂运算工具
"""

from mcp.server.fastmcp import FastMCP
from typing import Union

# 创建 MCP 服务实例
mcp = FastMCP("Basic Calculator Tools")

# ============================================================================
# 基础数学运算工具
# ============================================================================

@mcp.tool()
def add(a: float, b: float) -> float:
    """Add two numbers

    Args:
        a: First number
        b: Second number

    Returns:
        Sum of a and b
    """
    return a + b

@mcp.tool()
def subtract(a: float, b: float) -> float:
    """Subtract second number from first

    Args:
        a: Minuend (number to subtract from)
        b: Subtrahend (number to subtract)

    Returns:
        Difference (a - b)
    """
    return a - b

@mcp.tool()
def multiply(a: float, b: float) -> float:
    """Multiply two numbers

    Args:
        a: First factor
        b: Second factor

    Returns:
        Product of a and b
    """
    return a * b

@mcp.tool()
def divide(a: float, b: float) -> float:
    """Divide first number by second

    Args:
        a: Dividend (number to be divided)
        b: Divisor (number to divide by)

    Returns:
        Quotient (a / b)

    Raises:
        ValueError: If b is zero
    """
    if b == 0:
        raise ValueError("Division by zero is not allowed")
    return a / b

@mcp.tool()
def modulus(a: float, b: float) -> float:
    """Calculate remainder of division

    Args:
        a: Dividend (number to be divided)
        b: Divisor (number to divide by)

    Returns:
        Remainder of a divided by b

    Raises:
        ValueError: If b is zero
    """
    if b == 0:
        raise ValueError("Modulus by zero is not allowed")
    return a % b

@mcp.tool()
def power(base: float, exponent: float) -> float:
    """Raise base to the power of exponent

    Args:
        base: Base number
        exponent: Exponent to raise base to

    Returns:
        base raised to the power of exponent
    """
    return base ** exponent

# ============================================================================
# 启动服务
# ============================================================================

if __name__ == "__main__":
    # 使用 stdio 传输方式启动服务
    mcp.run(transport="stdio")